package cn.jcby.app.util.handler;

import cn.jcby.app.lebu.utils.Result;
import com.alibaba.fastjson.JSONObject;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {

    private final String headerValue;

    public CustomAuthenticationEntryPoint(String headerValue) {
        this.headerValue = headerValue;
    }

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/javascript;charset=utf-8");
        //response.getWriter().print(JSONObject.toJSONString(Result.error("token失效或token认证失败")));
        response.setStatus(401);
        response.getWriter().print(JSONObject.toJSONString(Result.error(502,"认证失败或者token已失效!")));
    }

}
